/* internal/js/menu-item/maintain.js */

/* Common */

var SERVLET_PATH = '/ordrs/internal/maintain-menu-item'; 

/* END Common */


/* Create */

function createMenuItem (itemAdded) {
	createMenuItemValidate(itemAdded);
}

//done
function createMenuItemValidate (itemAdded) {
	var number = document.getElementById('number').value;
	var name = document.getElementById('name').value;
	var price = document.getElementById('price').value;
	var menuID = document.getElementById('menu')
			.options[document.getElementById('menu').selectedIndex].value;

	var valid = true;
	
	if (number == '') {
		showFieldRequired('numberMessage');
		valid = false;
	}
	else if (!isNumeric(price)) { // not a number
		showFieldMessage('numberMessage','Must be number');
		valid = false;
	}
	else {
		hideFieldMessage('numberMessage');
	}
	
	if (name == '') { //left blank
		showFieldRequired('nameMessage');
		valid = false;
	}
	else {
		hideFieldMessage('nameMessage');
	}
	
	if (price == '') { // left blank
		showFieldRequired('priceMessage');
		valid = false;
	}
	else if (!isNumeric(price)) { // not a number
		showFieldMessage('priceMessage','Must be number');
		valid = false;
	}
	else {
		hideFieldMessage('priceMessage');
	}
	
	if (valid)
		createMenuItemSend(number, name, price, menuID, itemAdded);
}

//done
function createMenuItemSend (number, name, price, menuID, itemAdded) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('c') +
				'&number=' + encodeURI(number) +
	            '&name=' + encodeURI(name) +
	            '&price=' + encodeURI(price) +
	            '&menuid=' + encodeURI(menuID);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				createMenuItemReceive(req.responseXML, itemAdded);
			} else if (req.status == 204) {
				//can't find server
				createMenuItemReceive(encodeErrorXML('-1', 'Cannot find server.'), false);
			}
		}
	};
	sendPost(req, url, params);
}

function createMenuItemReceive (xml, itemAdded) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	var menuItemID = getErrorDetail(xml);
	if (code == 0) {
		if (itemAdded) {
			document.getElementById('menuItemID').value = menuItemID;
			addItem();
		}
		else
			reportMenuItem(menuItemID);
		showDialog('New Menu Item created.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Create */

/* Report */

function reportMenuItem(menuItemID) {
	reportMenuItemValidate(menuItemID);
}

//done
function reportMenuItemValidate (menuItemID) {
	try {
		menuItemID.length;
	} catch(e) {
		menuItemID = '';
	}
	var number = document.getElementById('number').value;
	var name = document.getElementById('name').value;
	var price = document.getElementById('price').value;
	reportMenuItemSend(menuItemID, number, name, price);
}

//done
function reportMenuItemSend (menuItemID, number, name, price) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('r') +
				'&id=' + encodeURI(menuItemID) +
				'&number=' + encodeURI(number) +
				'&name=' + encodeURI(name) +
	            '&=price' + encodeURI(price);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				reportMenuItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				reportMenuItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function reportMenuItemReceive (xml) {
	if (xml.getElementsByTagName('menuitems').length > 0) {
		var menuitems = xml.getElementsByTagName('menuitems')[0];
		showResults(buildTable(menuitems));
		if (menuitems.childNodes.length == 1) {
			var id = menuitems.childNodes[0].getElementsByTagName('id')[0].childNodes[0].nodeValue;
			showResult(id);
			listFoodItemInMI(id);
		}
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10101)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END Report */


/* Update */

function updateMenuItem() {
	updateMenuItemValidate();
}

function updateMenuItemValidate () {
	var id = document.getElementById('menuItemID').value;
	var number = document.getElementById('number').value;
	var name = document.getElementById('name').value;
	var price = document.getElementById('price').value;
	var menuID = document.getElementById('menu')
				.options[document.getElementById('menu').selectedIndex].value;

	if (name == '') { //left blank
		showFieldRequired('nameMessage');
	}
	else {
		hideFieldMessage('nameMessage');
		updateMenuItemSend(id, number, name, price, menuID);
	}
}

//really done
function updateMenuItemSend (id, number, name, price, menuID) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('u') +
	            '&id=' + encodeURI(id) +
	            '&number=' + encodeURI(number) +
	            '&name=' + encodeURI(name) +
	            '&price=' + encodeURI(price) +
	            '&menuid=' + encodeURI(menuID);
	
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				updateMenuItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				updateMenuItemReceive(encodeErrorXML('-1', 'Cannot find server.'), false);
			}
		}
	};
	sendPost(req, url, params);
}


function updateMenuItemReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		listAllMenuItemSend();
		showDialog('Menu Item updated.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Update */


/* Delete */

function deleteMenuItem() {
	openConfirmModalDialog('Please Confirm',
			'Are you sure you want to delete this Menu Item?','deleteMenuItemValidate');
}

function deleteMenuItemValidate () {
	var id = document.getElementById('menuItemID').value;
	if (id == '') {
		//no item selected
		showDialog('No Menu Item is selected for deletion.');
	} else {
		//item selected
		deleteMenuItemSend(id);
	}
}


function deleteMenuItemSend (id) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('d') +
	            '&id=' + encodeURI(id);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				/* returned code */
				deleteMenuItemReceive(req.responseXML);
			} else if (req.status == 204) {
				/* can't find server */
				deleteMenuItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function deleteMenuItemReceive (xml) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		resetFields();
		listAllMenuItemSend();
		showDialog('Menu Item deleted.');
	} else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Delete */


/* List All */

function listAllMenuItem () {
	listAllMenuItemSend();
	listFoodItem();
}

function listAllMenuItemSend () {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('l');
	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listAllMenuItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listAllMenuItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listAllMenuItemReceive (xml) {
	if (xml.getElementsByTagName('menuitems').length > 0) {
		var menus = xml.getElementsByTagName('menuitems')[0];
		showResults(buildTable(menus));
		if (menus.childNodes.length == 1) {
			var id = menus.childNodes[0].getElementsByTagName('id')[0].childNodes[0].nodeValue;
			showResult(id);
			//listFoodItemInMI(id);
		}
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List All */


/* List Food Item In Menu Item */

function listFoodItemInMI (menuItemID) {
	listFoodItemInMISend(menuItemID);
}

function listFoodItemInMISend (menuItemID) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('fimi') +
				 '&id=' + encodeURI(menuItemID);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listFoodItemInMIReceive(req.responseXML, menuItemID);
			} else if (req.status == 204) {
				//can't find server
				listFoodItemInMIReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listFoodItemInMIReceive (xml, menuItemID) {
	if (xml.getElementsByTagName('fooditems').length > 0) {
		var fooditems = xml.getElementsByTagName('fooditems')[0];
		document.getElementById('foodItemInMIList').innerHTML = buildFoodItemTable('FoodItemInMI',fooditems);
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List Food Item In Menu Item */


/* List Food Item */

function listFoodItem () {
	listFoodItemSend();
}


function listFoodItemSend () {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('lf');

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listFoodItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listFoodItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listFoodItemReceive (xml) {
	if (xml.getElementsByTagName('fooditems').length > 0) {
		var fooditems = xml.getElementsByTagName('fooditems')[0];
		document.getElementById('foodItemList').innerHTML = buildFoodItemTable('FoodItem', fooditems);
		if (xml.getElementsByTagName('fooditems').length == 0)
			selectFoodItemRow(0);
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10201)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END List Food Items */


/* Add an Item to Menu */

function addItem() {
	addItemValidate();
}

function addItemValidate() {
	var menuItemID = document.getElementById('menuItemID').value;
	var foodItemID = document.getElementById('foodItemID').value;
	if (menuItemID == '') {
		//no menu selected, create a new menu
		createMenuItem(true);
	} else if (menuItemID == '') {
		showDialog('No Food Item is selected on the right.');
	} else {
		//item selected
		addItemSend(menuItemID, foodItemID);
	}
}

function addItemSend (menuItemID, foodItemID) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('ai') +
				 '&menuitemid=' + encodeURI(menuItemID) +
				 '&fooditemid=' + encodeURI(foodItemID);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				addItemReceive(req.responseXML, menuItemID);
			} else if (req.status == 204) {
				//can't find server
				addItemReceive(encodeErrorXML('-1', 'Cannot find server.'), menuItemID);
			}
		}
	};
	sendPost(req, url, params);
}

function addItemReceive (xml, menuItemID) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		reportMenuItem(menuItemID);
		listFoodItem();
	}
	else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}

/* END Add an Item to Menu */


/* Remove an Item From Menu */

function removeItem() {
	removeItemValidate();
}

function removeItemValidate() {
	var menuItemID = document.getElementById('menuItemID').value;
	var foodItemID = document.getElementById('foodItemInMIID').value;
	if (menuItemID == '') {
		//no menu selected
		showDialog('No Menu Item is selected.');
	} else if (foodItemID == '') {
		showDialog('No Food Item is selected on the right.');
	} else {
		//item selected
		removeItemSend(menuItemID, foodItemID);
	}
}

function removeItemSend (menuItemID, foodItemID) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('ri') +
				 '&menuitemid=' + encodeURI(menuItemID) +
				 '&fooditemid=' + encodeURI(foodItemID);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				removeItemReceive(req.responseXML, menuItemID);
			} else if (req.status == 204) {
				//can't find server
				removeItemReceive(encodeErrorXML('-1', 'Cannot find server.'), menuItemID);
			}
		}
	};
	sendPost(req, url, params);
}


function removeItemReceive (xml, menuItemID) {
	var code = getErrorCode(xml);
	var message = getErrorMessage(xml);
	if (code == 0) {
		reportMenuItem(menuItemID);
		listFoodItem();
	}
	else if (code == -1)
		showDialog(message);
	else
		showDialog('Error ' + getErrorString(xml));
}


/* END Remove an Item From Menu */

/* Search for a Food Item */

function searchFoodItem() {
	searchFoodItemValidate();
}

//done
function searchFoodItemValidate () {
	var name = document.getElementById('foodItemName').value;
	var type = document.getElementById('foodItemType').value;
	searchFoodItemSend(name, type);
}

//done
function searchFoodItemSend (name, type) {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('si') +
				'&name=' + encodeURI(name) +
				'&type=' + encodeURI(type);

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				searchFoodItemReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				searchFoodItemReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}

function searchFoodItemReceive (xml) {
	if (xml.getElementsByTagName('fooditems').length > 0) {
		var foodItems = xml.getElementsByTagName('fooditems')[0];
		document.getElementById('foodItemList').innerHTML = buildFoodItemTable('FoodItem', foodItems);
		if (menuItems.childNodes.length == 1) {
			var id = menuItems.childNodes[0].getElementsByTagName('id')[0].childNodes[0].nodeValue;
			selectFoodItemRow(id);
		}
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10101)
			showResults(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}

/* END Search for a Food Item */

/* List Menu */

function listMenus () {
	listMenusSend();
}

function listMenusSend () {
	var url = SERVLET_PATH;
	var params = 'a=' + encodeURI('m');

	var req = getXmlHttpObject();
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				//returned code
				listMenusReceive(req.responseXML);
			} else if (req.status == 204) {
				//can't find server
				listMenusReceive(encodeErrorXML('-1', 'Cannot find server.'));
			}
		}
	};
	sendPost(req, url, params);
}


function listMenusReceive (xml) {
	if (xml.getElementsByTagName('menus').length > 0) {
		var menus = xml.getElementsByTagName('menus')[0];
		for (var loop = 0; loop < menus.childNodes.length; loop++) {
			var menu = menus.childNodes[loop];
			var id = menu.getElementsByTagName('id')[0].childNodes[0].nodeValue;
			var name = menu.getElementsByTagName('name')[0].childNodes[0].nodeValue;
			var optgroup = document.getElementById('menuOptGroup');
			var opt = document.createElement('option');
			opt.setAttribute('value', id);
			opt.innerHTML = name;
			optgroup.appendChild(opt);
		}
	} else {
		var code = getErrorCode(xml);
		var message = getErrorMessage(xml);
		if (code == 10301)
			showDialog(message);
		else if (code == -1)
			showDialog(message);
		else
			showDialog('Error ' + getErrorString(xml));
	}
}





/* END List Menu */

/* Format check */

function isNumeric(strToCheck) {
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strToCheck.length == 0) 
		return false;

	//  test strString consists of valid characters listed above
	for (var i = 0; i < strToCheck.length && blnResult; i++) {
	strChar = strToCheck.charAt(i);
   	if (strValidChars.indexOf(strChar) == -1)
	   blnResult = false;
	}
	return blnResult;
}

/* END Format check */